<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 使数组互补的最少操作次数 -->
    <script>
      var minMoves = function (nums, limit) {
        let diff = new Array(2 * limit + 2).fill(0)
        let n = nums.length
        let size = n >> 1
        for (let i = 0; i < size; i++) {
          let [pre, tail] = [nums[i], nums[n - 1 - i]]
          let [minNum, maxNum] = [Math.min(pre, tail), Math.max(pre, tail)]
          //相当于[2, 2*limit] 都加2;
          diff[2] += 2
          diff[2 * limit + 1] -= 2
          //相当于[1 + minNum, limit + maxNum] 都减1;
          diff[1 + minNum] += -1
          diff[limit + maxNum + 1] -= -1
          //相当于pre + tail 加1;
          diff[pre + tail] += -1
          diff[pre + tail + 1] -= -1
        }
        let result = (sum = diff[2])
        for (let i = 3; i <= 2 * limit; i++) {
          sum += diff[i] //求前缀和, 相当于还原数组;
          result = Math.min(result, sum) //求所有操作完成后 数组中最小的值;
        }
        return result
      }
      console.log(minMoves([1, 2, 4, 3], 4))
    </script>
  </body>
</html>
